package edu.lit.wantwords.controller;


import edu.lit.wantwords.entity.Users;
import edu.lit.wantwords.service.UsersService;
import io.swagger.annotations.Api;
import org.springframework.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.lit.wantwords.entity.Words;
import edu.lit.wantwords.entity.vo.WordQuery;
import edu.lit.wantwords.service.WordsService;
import edu.lit.wantwords.utils.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yc
 * @since 2022-06-24
 */
@Api(description = "单词操作")
@RestController
@RequestMapping("/word/words")
@CrossOrigin
public class WordsController {

    @Autowired
    private WordsService wordsService;
    @Autowired
    private UsersService usersService;

    @ApiOperation(value = "分页查询单词")
    @GetMapping("getWords/{currentPage}/{size}")
    public R getWords(@PathVariable Long currentPage,
                      @PathVariable Long size) {

        Page<Words> page = new Page<Words>(currentPage, size);
        wordsService.page(page, null);
        List<Words> wordsList = page.getRecords();
        long total = page.getTotal();
        return R.ok().data("list", wordsList).data("total", total);
    }

    @ApiOperation(value = "分页条件查询单词列表")
    @PostMapping("getWordsPageVo/{currentPage}/{size}")
    public R getWordsPageVo(@PathVariable Long currentPage,
                           @PathVariable Long size,
                           @RequestBody WordQuery wordsQuery) {
        LambdaQueryWrapper<Words> lqw = new LambdaQueryWrapper<Words>();
        String word = wordsQuery.getWordStr();
        String tran = wordsQuery.getTranStr();
        lqw.like(!StringUtils.isEmpty(word), Words::getWord, word);
        lqw.like(!StringUtils.isEmpty(tran), Words::getTranslation, tran);
        Page<Words> page = new Page<>(currentPage, size);
        wordsService.page(page, lqw);
        List<Words> wordsList = page.getRecords();
        long total = page.getTotal();
        return R.ok().data("list", wordsList).data("total", total);
    }

    @ApiOperation(value = "查询单词")
    @PostMapping("searchWord/{word}")
    public R searchWord(@PathVariable String word) {
        List<Words> list = new ArrayList<>();
        Words wordsServiceOne = this.getWords(word);
        if ( wordsServiceOne != null ) {
            list.add(wordsServiceOne);
            return R.ok().data("list", list).data("total", list.size());
        } else {
            List<Words> wordsList = wordsService.list(new LambdaQueryWrapper<Words>()
                    .like(Words::getWord, word)
                    .last("limit 5"));
            return R.ok().data("list", wordsList).data("total", wordsList.size());
        }
    }

    @ApiOperation(value = "单词查询次数加一")
    @GetMapping("searchCountAdd/{word}")
    public R searchCountAdd(@PathVariable String word) {
        Words words = this.getWords(word);
        if ( words != null ) {
            words.setSearchCount(words.getSearchCount() + 1);
            boolean b = wordsService.updateById(words);
            if ( b ) return R.ok();
            return R.error();
        }
        return R.error();
    }

    @ApiOperation(value = "添加生词本数加一")
    @GetMapping("addRowBook/{word}")
    public R addRowBook(@PathVariable String word) {
        Words words = this.getWords(word);
        if ( words != null ) {
            words.setSearchCount(words.getSearchCount() + 1);
            boolean b = wordsService.updateById(words);
            if ( b ) return R.ok();
            return R.error();
        }
        return R.error();
    }

    @ApiOperation(value = "获取单词列表")
    @GetMapping("getWordList/{userName}")
    public R getWordList(@PathVariable String userName) {
        Users users = usersService.getOne(new LambdaQueryWrapper<Users>()
                .eq(Users::getUserName, userName));
        Integer userNextWord = users.getUserFinishedNum();
        if ( userNextWord == null ) {
            userNextWord = 1;
        }
        List<Words> wordsList = wordsService.list(new LambdaQueryWrapper<Words>()
                .between(Words::getSortNum, userNextWord, userNextWord + 19));
        return R.ok().data("wordsList", wordsList);
    }

    public Words getWords(String word) {
        Words words = wordsService.getOne(new LambdaQueryWrapper<Words>()
                .eq(Words::getWord, word));
        return words;
    }
}

